<?php
declare(strict_types=1);

namespace Service\MemberService;
use Service\BaseService;
use App\Model\BbcModel\Vip;
use App\Model\BbcModel\Mnuser;
use Service\Code\Billcode;
use App\Model\BbcModel\VipLog;
use Service\UserService\MnUserService;
use App\Model\BbcModel\Member;

class VipService extends BaseService
{
    /**
     * 注册来源
     * @var array
     */
    public $vip_source = [
        0   => '购买',
        1   => '赠送',
        2   => '注册赠送'
    ];

    /**
     * 检查单个用户是否为黑卡会员
     * @param [type] $mid
     * @return array
     */
    public function checkSingleUserIsBlackVipCard($mid,$mn_sid)
    {
        $mnUser = MnUserService::instance()->getMnUserInfoByMnSid($mn_sid);
        $isVipArr = VipService::instance()->checkUserIsBlackVipCard($mnUser, [$mid]);
        return current($isVipArr);
    }
    
    /**
     * 批量检查用户在当前店铺是否是黑卡
     * @param array $mn_user
     * @param array $mids
     * @return mixed
     */
    public function checkUserIsBlackVipCard(array $mn_user, array $mids = [])
    {
        $mn_sid = $mn_user['id'];
        $time = time();
        
        $vipNoneExpireMemberList = Vip::instance()->where('expire_time','>',$time)->where('mn_sid',$mn_sid)->whereIn('mid',$mids)->get()->toArray();
        $vipNoneExpireMemberListMap = array_column($vipNoneExpireMemberList,null,'mid');
        $vipExpireAtMemberList = Vip::instance()->where('expire_time','<',$time)->where('mn_sid',$mn_sid)->whereIn('mid',$mids)->get()->toArray();
        $vipExpireAtMemberListMap = array_column($vipExpireAtMemberList,null,'mid');
        
        $channel_mn_user = Mnuser::instance()->select(['id as mn_sid'])->where('member_share',1)->where('status',1)->where('new_store',1)->where('pid',$mn_user['pid'])->get()->toArray();
        
        $mn_sids = array_column($channel_mn_user,'mn_sid');
        
        //获取其它店铺不过期的情况
        $select = ['id','expire_time','type'];
        $otherVipNoneExpireMemberList = Vip::instance()->select($select)->where('expire_time','>',$time)->whereIn('mn_sid',$mn_sids)->whereIn('mid',$mids)->get()->toArray();
        $otherVipNoneExpireMemberListMap = array_column($otherVipNoneExpireMemberList,null,'mid');
        
        //获取其它店铺过期的情况
        $otherVipExpireAtMemberList = Vip::instance()->select($select)->where('expire_time','<',$time)->whereIn('mn_sid',$mn_sids)->whereIn('mid',$mids)->get()->toArray();
        $otherVipExpireAtMemberListMap = array_column($otherVipExpireAtMemberList,null,'mid');
        
        $vip_new_list = [];
        foreach ($mids as $mid) 
        {
            //没有过期的用户叫做会员用户
            if( isset($vipNoneExpireMemberListMap[$mid]) ) 
            {
                $has = $vipNoneExpireMemberListMap[$mid];
                $vip_info = [
                    'is_vip'        => 1,
                    'expire_time'   => $has['expire_time'],
                    'type'          => $this->vip_source[$has['type']]
                ];
            } 
            else 
            {
                //如果当前店铺不是会员或者已经过期所在的渠道下的其它店铺是会员的情况下，如果是当前店铺设置了共享会员，则会强制设置为会员
                if( $mn_user['status']== 1 && $mn_user['member_share']== 1 ) 
                {
                    if( isset($otherVipNoneExpireMemberListMap[$mid]) ) {
                        $has = $otherVipNoneExpireMemberListMap[$mid];
                        $vip_info = [
                            'is_vip'        => 1,
                            'expire_time'   => $has['expire_time'],
                            'type'          => $this->vip_source[$has['type']]
                        ];
                    } else if( isset($otherVipExpireAtMemberListMap[$mid]) ) {
                        $has2_expired = $otherVipExpireAtMemberListMap[$mid];
                        $vip_info = [
                            'is_vip'        => 0,
                            'expire_time'   => 0,
                            'expired'       => 1,
                            'expired_time'  => $has2_expired['expire_time']
                        ];
                    } else {
                        $vip_info = [
                            'is_vip'        => 0,
                            'expire_time'   => 0,
                            'expired'       => 0
                        ];
                    }
                } else {
                    $vip_info = [
                        'is_vip'        => 0,
                        'expire_time'   => 0,
                        'expired'       => 0
                    ];
                }
            }
            
            $vip_info['mid'] = $mid;
            array_push($vip_new_list, $vip_info);
        }
        return $vip_new_list;
    }
    
    /**
     * 收银机增加赠送会员
     * @param int $mid
     * @param int $mn_sid
     * @return boolean
     */
    public function addVipByCashAuto(int $mid,int $mn_sid) {
        $time=time();
        $sn=Billcode::returnCode('V', $mn_sid);
        $day=365;
        $vipOrder=[
            'mn_sid'=>$mn_sid,
            'sn'=>$sn,
            'type'=>3,
            'amount'=>0,
            'days'=> $day,
            'cd'=>$time,
            'pay_time'=>$time,
            'pay_status'=>1,
            'mid'=>$mid,
            'is_online'=>0,
            'pay_rate'=>0
        ];
        $vip=[
            'mid'=>$mid,
            'mn_sid'=>$mn_sid,
            'start_time'=>$time,
            'cd'=>$time,
            'type'=>1,
            'expire_time'=>strtotime(date('Y-m-d',$time))+($day+1)*86400
        ];
        $viplog= VipLog::instance()->add($vipOrder);
        $vipinfo= Vip::instance()->info(['mid'=>$mid]);
        if($vipinfo){
            $editvipinfodata=[];
            if($vipinfo['expire_time']>$time){
                $editvipinfodata['expire_time']=strtotime(date('Y-m-d',$vipinfo['expire_time']))+($day+1)*86400;
            }else{
                $editvipinfodata['expire_time']=strtotime(date('Y-m-d',$time))+($day+1)*86400;
            }
            $editvipinfodata['type']=$vip['type'];
            $editvipinfodata['md']=$time;
            $exp_time=$editvipinfodata['expire_time'];
            $vipres=Vip::instance()->edit(['id'=>$vipinfo['id']],$editvipinfodata);
        }else{
            $exp_time=$vip['expire_time'];
            $vipres=Vip::instance()->add($vip);
        }
        Member::instance()->edit(['mid'=>$mid],['vip_level'=>1,'vip_expired'=>$exp_time,'vip_type'=>1]);
        return true;
    }
}
